
<div class="common-tit">
  <h2>添加意向学员</h2>
  <span>招生管理 > 编辑意向学员</span>
</div>
<div class="cur-box">
  <h3>
    <span>基本信息</span>
  </h3>
  <div class="cur-basic clearfix">
    <div class="ads-info clearfix">
      <div  id="upload_avatar_container"  class="camera">
        <div class="ca">
          <% if @potential_student.avatar.present? %>
            <%= image_tag("#{@potential_student.avatar_url}", size: "150x150", alt: "头像" ,id: "potential_student_avatar_img")%>
          <% else %>
            <%= image_tag("/avatar.png", size: "150x150", alt: "头像" ,id: "potential_student_avatar_img")%>   
          <% end %>  
        </div>
        <div id="upload_avatar" class="upl">本地上传</div>
        <input type="hidden" id="potential_student_avatar" value="">
      </div>
      <ul class="bs-box">
        <li>
          <input type="text" class="in-l required" placeholder="姓名" value="<%= @potential_student.name %>">
          <input type="text" class="in-r" placeholder="别名 / 英文名" value="<%= @potential_student.alias_name %>">
        </li>
        <li>
          <div class="list-box b-sex" choice="mandatory">
            <div class="downbtn">
              <span data_id="<%= @potential_student.sex %>"><%= @potential_student.sex %></span>
              <em></em>
            </div>
            <ul class="dropdown-menu">
              <% Company::GENDER.each do |key,value| %>
                <li data_id="<%= key %>"><%= value %></li>
              <%end%>
            </ul>
          </div>
          <b>请选择</b>
          <div class="list-box group spe clearfix" choice="mandatory">
            <div class="downbtn">
              <span data_id="<%= @potential_student.student_group %>"><%= @potential_student.student_group ? @potential_student.student_group_configuration.group_name : "学员分组"  %></span>
              <em></em>
            </div>
            <ul class="dropdown-menu">
              <li class="cho-tit">学员分组</li>
              <% @student_group.each do|sg| %>
                <li data_id="<%= sg.id %>"><%= sg.group_name %></li>
              <% end %>
            </ul>
          </div>
        </li>
        <li>
          <input id="stuTel" type="text" class="in-l required" placeholder="手机号" value="<%= @potential_student.mobile %>" readonly="readonly">
          <input type="text" class="in-r" placeholder="微信号" value="<%= @potential_student.weixin %>" >
        </li>
        <li>
          <div class="date-ibox dr">
            <input type="text" class="in-l date-input" readOnly="true" placeholder="出生年月" value="<%= @potential_student.birthday %>">
          </div>
          <div class="list-box group" choice="mandatory">
            <div class="downbtn">
              <span data_id="<%= @potential_student.source_from %>"><%= @potential_student.source_from ? @potential_student.source_from : "信息来源"  %></span>
              <em></em>
            </div>
            <ul class="dropdown-menu">
              <li class="cho-tit">信息来源</li>
              <% Company::SOURCE_FROM.each do |key,value| %>
                <li data_id="<%= key %>"><%= value %></li>
              <% end %>
            </ul>
          </div>
        </li>
        <li>
          <input type="text" class="in-l" placeholder="紧急联系人姓名" value="<%= @potential_student.emergency_contact %>">
          <input type="text" class="in-r" placeholder="紧急联系人电话" value="<%= @potential_student.emergency_mobile %>">
        </li>
        <li>
          <input type="text" class="in-a" placeholder="联系地址" value="<%= @potential_student.address %>">
        </li>
        <li>
          <input type="text" class="in-a" placeholder="学习需求" value="<%= @potential_student.learning_requirement %>">
        </li>
        <li>
          <input type="text" class="in-a" placeholder="经济状况" value="<%= @potential_student.state_of_business %>">
        </li>
        <li>
          <input type="text" class="in-a" placeholder="学习意愿" value="<%= @potential_student.learning_intention %>">
        </li>
      </ul>	
    </div>
  </div>
  <h3>
    <span>相关信息</span>
  </h3>
  <div class="ad-stu clearfix">
    <div class="multiselect-box">
      <div class="mul-downbtn">
        <em></em>
        <input type="text" placeholder="搜索">
      </div>
      <div class="multiselect-down">
        <ul>
          <% @teachers.each do |teacher| %>
          <li class="<%= 
          # 修复没有意向导师split的错误，kevin
          if @potential_student.willing_teacher
            @potential_student.willing_teacher.split(",").include?(teacher.name) ? "mul-select" : ""
          else
            ""
          end  %>">
            <span></span>
            <strong><%= teacher.name %></strong>
          </li>
          <% end %>
        </ul>
        <div class="mul-btn clearfix">
          <div class="mul-confirm">确认</div>
          <div class="mul-cancel">取消</div>
        </div>
      </div>
    </div>
    <div class="list-box spe clearfix" choice="mandatory">
      <div class="downbtn">
        <span data_id="<%= @potential_student.willing_studio%>"><%= @potential_student.willing_studio ? @potential_student.intention_studio.name : "意向分店"  %></span>
        <em></em>
      </div>
      <ul class="dropdown-menu">
        <li class="cho-tit">意向分店</li>
        <% @studios.each do |studio| %>
          <li data_id="<%= studio.id %>"><%= studio.name %></li>
        <% end %>
      </ul>
    </div>
  </div>
  <%= tag(:input, :type => 'hidden', :name => 'utf-8',:value => "✓") %>
  <%= tag(:input, :type => "hidden", :name =>  request_forgery_protection_token.to_s, :value =>   form_authenticity_token) %> 
  <div class="intention-btn">保存</div>
</div>
<script src="/assets/js/common.js"></script>
<script>
$(function(){
  $('.date-list1').date_input();
  $(".date-list2").date_input();
  $(".date-input").date_input();
});
$('.date-list2').on("click", function() {
  updateT1();
  $('.nav span').on("click", function() {
    if ($(this).parent().parent().parent().prev().attr("class") == "date-list2") {
      updateT1();
    };
  });
});
$('.date-list1').on("click", function() {
  updateT2();
  $('.nav span').on("click", function() {
    if ($(this).parent().parent().parent().prev().attr("class") == "date-list1") {
      updateT2();
    };
  });
});
$(".intention-btn").on("click", function() {
  if(adstuSet()) {
    var postJson = {
      "potential_student_id" : "<%= @potential_student.id %>",
      "potential_student" : [{
        "name" : $(".bs-box input").eq(0).val(),
        "alias_name" : $(".bs-box input").eq(1).val(),
        "sex" : $(".b-sex").find("span").text(),
        "student_group" : $(".bs-box .list-box").eq(1).find("span").attr("data_id"),
        "mobile" : $(".bs-box input").eq(2).val(),
        "weixin" : $(".bs-box input").eq(3).val(),
        "birthday" : $(".bs-box input").eq(4).val(),
        "source_from" : $(".bs-box .list-box").eq(2).find("span").text(),
        "emergency_contact" : $(".bs-box input").eq(5).val(),
        "emergency_mobile" : $(".bs-box input").eq(6).val(),
        "address" : $(".bs-box input").eq(7).val(),
        "learning_requirement" : $(".bs-box input").eq(8).val(),
        "state_of_business" : $(".bs-box input").eq(9).val(),
        "learning_intention" : $(".bs-box input").eq(10).val(),
        "willing_studio" : $(".ad-stu .list-box").find("span").attr("data_id"),
        "avatar" : $("#potential_student_avatar").val(),
        "willing_teacher" : []
      }],
      'utf-8': $("input[name=utf-8]").val(),
      'authenticity_token': $("input[name=authenticity_token]").val()
    };
    var arr = [];
    for (var i = 0; i < $(".ad-stu .multiselect-down").find(".mul-select").length; i++) {
      arr.push($($(".ad-stu .multiselect-down").find(".mul-select")[i]).children("strong").text());
    };
    postJson.potential_student[0].willing_teacher = arr;
  }
  console.log("提交内容为:"+JSON.stringify(postJson));
  console.log(postJson);
  
  $.ajax({
    data: postJson,
    type:'post', url:"/admin/potential_students/save_student",
    success: function(data) {
      if (data.error) {
        // console.log("wrong");
      } 
    }
  });

})
// 验证是否必填
function adstuSet() {
  var writeIn = true;
  for (var i = 0; i < $(".bs-box .required").length; i++) {
    if ($($(".required")[i]).val() == "") {
      $($(".required")[i]).css({
        "border" : "1px solid #ff5a60" 
      });
      writeIn = false;
    } else {
      $($(".required")[i]).css({
        "border" : "1px solid #9ea0b7" 
      });
    }
  }
 
  // 添加 ajax 如果
  return writeIn;
}

</script>
